<template>
  <div>
    <router-link to="/"
      class="header-abs"
      tag="div"
      v-show="showAbsHeader">
      <div class="iconfont header-abs-back">&#xe624;</div>
    </router-link>
    <div v-show="!showAbsHeader"
      :style="opacityStyle"
      class="header-fixed">
      {{ sightName }}
      <router-link to="/"
        tag="div"
        class="iconfont header-fixed-back">
        &#xe624;
      </router-link>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  props: {
    sightName: String
  },
  data () {
    return {
      showAbsHeader: true,
      scrollTop: 0
    }
  },
  computed: {
    opacityStyle () {
      return {
        opacity: this.scrollTop / (200 - 60)
      }
    }
  },
  methods: {
    handleScroll () {
      const top = document.documentElement.scrollTop || document.body.scrollTop
      this.showAbsHeader = top <= 60
      // 滚动位置距离顶部 60px ~ 200px 时
      // 透明度在0到1之间变化，实现渐隐渐现效果
      this.scrollTop = top >= 60 && top <= 200 ? (top - 60) : (200 - 60)
    }
  },
  activated () {
    window.addEventListener('scroll', this.handleScroll)
  },
  deactivated () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/variables.styl'
  @import '~styles/mixins.styl'
  .header-abs
    position absolute
    left .2rem
    top .2rem
    width .8rem
    height .8rem
    line-height .8rem
    text-align center
    border-radius .4rem
    background rgba(0, 0, 0, .5)
    .header-abs-back
      color #fff
  .header-fixed
    z-index 1
    position fixed
    left 0
    right 0
    top 0
    line-height $headerHeight
    height  $headerHeight
    padding 0 1rem
    text-align center
    font-size .32rem
    color #fff
    background $bgColor
    ellipsis()
    .header-fixed-back
      position absolute
      top 0
      left 0
      width .64rem
      font-size .4rem
      color #fff
</style>
